<?php
$_CONFIG['site_root'] = "http://localhost/mercatino-libri";
class db {
	
		
	function __construct() {
		$_CONFIG['host'] = "localhost";
		$_CONFIG['user'] = "root";
		$_CONFIG['pass'] = "piero";
		$_CONFIG['dbname'] = "mercatino";
	    $conn = mysql_connect($_CONFIG['host'], $_CONFIG['user'], $_CONFIG['pass']) or die('Impossibile stabilire una connessione');
		mysql_select_db($_CONFIG['dbname']);
   }
	function get_book($id){
		$result=mysql_query("SELECT id, title, author, publisher, EAN, subject, media
							 FROM books WHERE id='$id'");
		$row=mysql_fetch_array($result);
		return $row;
		
	}
	
	
	function get_all_books($subject = FALSE, $sortby = FALSE, $page = FALSE , $random = FALSE){
		
		if ( $random )
			$query="SELECT books.* FROM (SELECT FLOOR (RAND() * (SELECT count(*) FROM books )) num ,@num:=@num+1 from (SELECT @num:=0) a , books LIMIT 0, 18) b ,  books WHERE b.num=books.id  ;";
			
		else{
			$query="SELECT id, title, author, publisher, EAN, subject, media FROM books ";
			$count = "SELECT COUNT( * ) FROM books";
		}
		if ($subject){
			$query.=" WHERE `subject` LIKE \"$subject\"";
			$count .= " WHERE `subject` LIKE \"$subject\"";
		}
		if ($sortby)
			$query.=" ORDER BY $sortby";
		if (!$random && $page){
			 $limit = ($page-1) * 18;
			$query.=" LIMIT $limit, 18";
		}
			
		
		$result=mysql_query($query);
		if ($result){
			$books = array();
			
			$array = array();
			while($row = mysql_fetch_array($result)){
				$title = trim($row['title']);
				if (!empty($title))
	  				$array[]=$row;
	  		}
	  		$books['list'] = $array;
		  	if ($subject){
		  		$result = mysql_query($count);
		  		$row = mysql_fetch_array($result);
		  		$books['num_pages'] = ceil ($row['COUNT( * )'] / 18 );
		  	}
			return $books;
		}
		else return false;
	}
	
	function get_all_subjects(){
		$query="select distinct subject from books where subject is not null and subject<>'' order by subject
				asc";
		$result = mysql_query($query);
		if ($result){
			$array = array();
			while($row = mysql_fetch_array($result))
	  		{
	  			
	  			$array[] = $row['subject'];
	  		}
			
			return $array;
		}
		else return false;
		
		
	}
	function insert_book($book){
		
		$query="INSERT INTO books (`title`,
									`author`,
									`publisher`,
									`EAN`,
									`subject` ) 
				VALUES( '".$book['title']."','".
						$book['author']."','".
						$book['publisher']."','".
						$book['EAN']."','".
						$book['subject']."' ) ";
		$result = mysql_query($query);
		
	}
	
	
	function set_media($id, $b){
		if ( $b ){
			$query = "UPDATE books SET media = 1 WHERE id=$id";
			return mysql_query($query);
		}
		if ( $b == FALSE ){
			$query = "UPDATE books SET media = 0 WHERE id=$id";
			return mysql_query($query);
		}
		
		return FALSE;
	
	}
	
	function has_cover($id){
		$query = "SELECT media FROM books WHERE id=$id";
		$result = mysql_query($query);
		$result =  mysql_fetch_array($result);
		return $result['media'];
	}
	
function insert_request($request){
		$cart = new Cart();
		$cart_list = $cart->get_all();
		$json_list = array();
		
		if ( $cart->is_empty())
			return false;
		
			
		
		foreach ($cart_list as $book){
			$json_list[] = array("id"=> $book['id'] , "num_copies" => $book['num_copies']);
		}
		
		$json_list = json_encode($json_list) ;
		
		
		
		$query = "INSERT INTO book_requests (`book_list`,
									`email`,
									`phone`,
									`mobile`,
									`tax_code`,
									`name`,
									`surname`,
									`city`,
									`cap`,
									`address`,
									`additional`,
									`time`
									 ) 
				VALUES( '".$json_list."','".
						$request['email']."','".
						$request['phone']."','".
						$request['mobile']."','".
						$request['tax_code']."','".
						$request['name']."','".
						$request['surname']."','".
						$request['city']."','".
						$request['cap']."','".
						$request['address']."','".
						$request['additional']."','".
						date('Y-m-d H:i:s', time())."'".
						")";
		$result = mysql_query($query);
		
	}
	
	function get_all_requests(){
		$query="SELECT * FROM book_requests WHERE processed = 0; ";
		$result=mysql_query($query);
		if ($result){
			$array= array();
			while($row = mysql_fetch_array($result)){
				$array[]=$row;
	  		}
			
			return $array;
		}
		else return false;
		
		
	}
	
	function delete_request($id){
		$query = "DELETE FROM book_requests WHERE `id`=$id" ;
		return mysql_query($query);
	}
	
	function set_request_complete($id){
		$query = "UPDATE book_requests SET processed = 1 WHERE id=$id";
		return mysql_query($query);
		
	}
	
	function query ($query){
		
		error_log($query);
		$result = mysql_query($query);
		
		if ( is_bool($result))
			return $result;
		
		return mysql_fetch_array( $result );
		
			
	}
	
	
	
}
$db = new db();

?>